# 新建文件：flask_api.py
from flask import Flask, request, jsonify
from flask_cors import CORS  # 处理跨域
from chat_agent import EnhancedDBQA

app = Flask(__name__)
CORS(app)  # 允许所有跨域请求

@app.route('/ask', methods=['POST'])
def ask_question():
    data = request.json
    student_id = data.get('studentId')
    print(student_id)
    question = data.get('question')
    print(question)

    if not student_id or not question:
        return jsonify({"error": "缺少必要参数"}), 400

    try:
        qa_system = EnhancedDBQA(student_id)
        response = qa_system.generate_answer(question, student_id)
        print(response)
        return jsonify({"answer": response})
    except Exception as e:
        return jsonify({"error": str(e)}), 500


if __name__ == '__main__':
    app.run(port=5000, debug=True)